package com.itheima.advantagesup.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.advantagesup.common.PaginationVO;
import com.itheima.advantagesup.entity.SysMenu;
import com.itheima.advantagesup.entity.SysRole;
import com.itheima.advantagesup.entity.addRole;
import com.itheima.advantagesup.mapper.MenuMapper;
import com.itheima.advantagesup.mapper.SysMenuMapper;
import com.itheima.advantagesup.mapper.SysRoleMapper;
import com.itheima.advantagesup.service.SysRoleService;
import com.itheima.advantagesup.vo.MenuVO;
import com.itheima.advantagesup.vo.RoleVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class SysRoleServiceImpl implements SysRoleService {
    private final SysMenuMapper mapper;
    private final SysRoleMapper sysRoleMapper;

    public SysRoleServiceImpl(SysRoleMapper sysRoleMapper, SysMenuMapper mapper) {
        this.sysRoleMapper = sysRoleMapper;
        this.mapper = mapper;
    }

    @Override
    public PaginationVO<RoleVO> page(String page, String limit, String name) {
        PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(limit));
        List<SysRole> roles = sysRoleMapper.list(name);
        PageInfo<SysRole> pageInfo = new PageInfo<>(roles);
        long count = pageInfo.getTotal();
        int totalPages = pageInfo.getPages();
        int pageSize = pageInfo.getPageSize();
        int currentPage = pageInfo.getNextPage();
        List<SysRole> sysRoleList1 = pageInfo.getList();

        List<RoleVO> roleVOList = new ArrayList<>();
        sysRoleList1.forEach(sysRole -> roleVOList.add(sysRole.getRoleVO()));

        PaginationVO<RoleVO> paginationVO = new PaginationVO<>();
        paginationVO.setCount(count);
        paginationVO.setTotalPages(totalPages);
        paginationVO.setPageSize(pageSize);
        paginationVO.setCurrentPage(currentPage);
        paginationVO.setData(roleVOList);
        return paginationVO;
    }

    @Override
    public List<SysRole> allData() {
        List<SysRole> sysRoles = sysRoleMapper.allData();
        return sysRoles;
    }

    @Override
    public void delRoleById(String id) {
        sysRoleMapper.delRoleMenuById(id);
        sysRoleMapper.delRoleById(id);
    }

    @Override
    public RoleVO rowInfoRoleById(String id) {
        // 角色信息
        SysRole sysRole = sysRoleMapper.rowInfoRoleById(id);
        // 角色菜单列表信息
        List<SysMenu> sysMenuList = mapper.selectListByRoleIds(Collections.singletonList(sysRole.getId()));
        // 将角色信息变成树状结构
        List<MenuVO> sysMenus = covertVOTree(sysMenuList);
        RoleVO sysRoleVO = sysRole.getRoleVO();
        sysRoleVO.setAuthList(sysMenus);
        return sysRoleVO;
    }

    @Override
    public void addRole(addRole role) {
        UUID uuid = UUID.randomUUID();
        RoleVO roleVO = new RoleVO();
        roleVO.setId(uuid.toString());
        roleVO.setName(role.getName());
        roleVO.setDescription(role.getDescription());
        roleVO.setCreate_date(LocalDateTime.now());
        roleVO.setUpdate_date(LocalDateTime.now());
        roleVO.setDefault_data("1");

        Map<String, String> map = new TreeMap<>();
        Arrays.stream(role.getMenuids()).forEach(num -> {
            map.put(num, uuid.toString());
        });

        map.forEach((key, value) -> {
            addRole roles = new addRole();
            roles.setRoleId(value);
            roles.setMenuId(key);
            System.out.println(roles);
            sysRoleMapper.addRoleMenu(roles);
        });

        sysRoleMapper.addRole(roleVO);
    }

    @Override
    public void updateRole(addRole role) {
        System.out.println(role);
        RoleVO roleVO = new RoleVO();
        roleVO.setId(role.getId());
        roleVO.setName(role.getName());
        roleVO.setDescription(role.getDescription());
        roleVO.setUpdate_date(LocalDateTime.now());

        Map<String, String> map = new TreeMap<>();
        Arrays.stream(role.getMenuids()).forEach(num -> {
            map.put(num, role.getId());
        });

        System.out.println(map);
        sysRoleMapper.delRoleMenuById(role.getId());
        map.forEach((key, value) -> {
            addRole roles = new addRole();
            roles.setRoleId(value);
            roles.setMenuId(key);
            System.out.println(roles);
            sysRoleMapper.addRoleMenu(roles);
        });
        System.out.println("执行完了~");
        sysRoleMapper.updateRole(roleVO);
    }

    private List<MenuVO> covertVOTree(List<SysMenu> sysMenuList) {
        // 获取顶层菜单的逻辑 就是pid为0
        List<MenuVO> topLayerMenus = sysMenuList.stream()
                .filter(sysMenu -> sysMenu.getPid() == 0)
                .map(SysMenu::getMenv)
                .collect(Collectors.toList());
        topLayerMenus.forEach(sysMenuItem1 -> {
            // 获取第一层的子集
            List<MenuVO> menus2 = sysMenuList.stream()
                    .filter(sysMenu -> sysMenu.getPid().equals(sysMenuItem1.getId()))
                    .map(SysMenu::getMenv)
                    .collect(Collectors.toList());

            menus2.forEach(sysMenuItem2 -> {
                // 获取第二层的子集
                List<MenuVO> menus3 = sysMenuList.stream()
                        .filter(sysMenu -> sysMenu.getPid().equals(sysMenuItem2.getId()))
                        .map(SysMenu::getMenv)
                        .collect(Collectors.toList());
                sysMenuItem2.setChildren(menus3);
            });

            sysMenuItem1.setChildren(menus2);
        });
        return topLayerMenus;
    }

}

